# -*- coding: utf-8 -*-

# 导入pygame模块
import pygame
import random
import math

# 初始化硬件接口
pygame.init()

# 窗口基本信息
screen = pygame.display.set_mode((1280,718))
pygame.display.set_caption("Hello World!")

# ***切记：添加图片一定要在创建窗口之后***
# 添加背景图片
background_image_path = 'bg0.jpg'
background = pygame.image.load(background_image_path)

# 添加背景音乐
pygame.mixer.music.load("bg.wav")
pygame.mixer.music.play(-1)
# 添加游戏音效
eat_sound = pygame.mixer.Sound("eat.mp3")
success_sound = pygame.mixer.Sound("success.mp3")
# 添加鼠标图片
mouse_image_filename = '指针.png'
mouse_cursor = pygame.image.load(mouse_image_filename).convert_alpha()
# 添加boy
boy = pygame.image.load("./men/boy.png")
boyX = 624
boyStep = 0.5
boyY = 486
try:
    scores = 0
    font = pygame.font.Font("freesansbold.ttf",32)
    def show_score():
        global scores
        text = f"score: {scores}"
        score_render = font.render(text,True, (0,255,0))
        screen.blit(score_render, (10,10))
except:
    print("没有该字体")
    
try:
    
    font1 = pygame.font.Font("freesansbold.ttf",66)
    def show_success():
        global scores
        text = "Success"
        score_render = font1.render(text,True, (237,248,177,1))
        screen.blit(score_render, (520,340))
except:
    print("没有该字体")

# 添加一个水果
banana = pygame.image.load("./fruits/banana.png")
apple = pygame.image.load("./fruits/apple.png")
avocado = pygame.image.load("./fruits/avocado.png")
blueberry = pygame.image.load("./fruits/blueberry.png")
cherry = pygame.image.load("./fruits/cherry.png")
durian = pygame.image.load("./fruits/durian.png")
grape = pygame.image.load("./fruits/grape.png")
lemon = pygame.image.load("./fruits/lemon.png")
kiwi = pygame.image.load("./fruits/kiwi.png")
litchi = pygame.image.load("./fruits/litchi.png")
longan = pygame.image.load("./fruits/longan.png")
mango = pygame.image.load("./fruits/mango.png")
# 定义一个boy跟着mouse走的函数


fruits_imgs = [apple,banana,avocado,blueberry,cherry,durian,grape,kiwi,litchi,longan,mango]
class Fruits:
    def __init__(self, img):
        self.x = random.randint(0,1280)
        self.y = random.randint(0,680)
        self.img = img
    
fruits = []
for fruit in fruits_imgs:
    fruits.append(Fruits(fruit))


isrunning = True
while isrunning:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            isrunning = False
    screen.blit(background,(0,0))
    # 获取鼠标坐标
    x,y = pygame.mouse.get_pos()
    #计算光标的左上角位置
    x  -=  mouse_cursor.get_width()  /  2
    y  -=  mouse_cursor.get_height()  /  2
    screen.blit(mouse_cursor,(x,y))
    screen.blit(boy,(boyX,boyY))
    
    if (boyX-x)!=0:
        pro = (boyY - y)/(boyX - x)
        boyX += boyStep
        boyY += pro*boyStep
        if boyX < x and boyY > y:
            boyStep = 0.5
        if boyX > x and boyY > y:
            boyStep = -0.5
            pro *= -1
        if boyX > x and boyY < y:
            boyStep = -0.5
            pro *= -1
        if boyX < x and boyY < y:
            boyStep = 0.5
    for i in fruits:
        screen.blit(i.img,(i.x, i.y))
        a = i.x - boyX
        b = i.y - boyY
        if math.sqrt(a*a + b*b)<10:
            eat_sound.play()
            fruits.remove(i)
            scores += 1
            print(scores)
    if len(fruits) == 0:
        
        success_sound.play()
        show_success()    
    
    show_score()
    
    pygame.display.update()
            
pygame.quit()